package com.bluesky.vnes;

public class Keyboard {

	public class KeyEvent {
		public byte keyCode;
		public boolean preventDefault;

		public void preventDefault() {

		}
	}

	static int KEY_A = 0;
	static int KEY_B = 1;
	static int KEY_SELECT = 2;
	static int KEY_START = 3;
	static int KEY_UP = 4;
	static int KEY_DOWN = 5;
	static int KEY_LEFT = 6;
	static int KEY_RIGHT = 7;

	public byte[] state1 = new byte[8];
	public byte[] state2 = new byte[8];

	public Keyboard() {
		int i;

		for (i = 0; i < this.state1.length; i++) {
			this.state1[i] = 0x40;
		}

		for (i = 0; i < this.state2.length; i++) {
			this.state2[i] = 0x40;
		}
	}

	public boolean setKey(byte key, byte value) {
		switch (key) {
		case 88:
			this.state1[KEY_A] = value;
			break; // X
		case 90:
			this.state1[KEY_B] = value;
			break; // Z
		case 17:
			this.state1[KEY_SELECT] = value;
			break; // Right Ctrl
		case 13:
			this.state1[KEY_START] = value;
			break; // Enter
		case 38:
			this.state1[KEY_UP] = value;
			break; // Up
		case 40:
			this.state1[KEY_DOWN] = value;
			break; // Down
		case 37:
			this.state1[KEY_LEFT] = value;
			break; // Left
		case 39:
			this.state1[KEY_RIGHT] = value;
			break; // Right

		case 103:
			this.state2[KEY_A] = value;
			break; // Num-7
		case 105:
			this.state2[KEY_B] = value;
			break; // Num-9
		case 99:
			this.state2[KEY_SELECT] = value;
			break; // Num-3
		case 97:
			this.state2[KEY_START] = value;
			break; // Num-1
		case 104:
			this.state2[KEY_UP] = value;
			break; // Num-8
		case 98:
			this.state2[KEY_DOWN] = value;
			break; // Num-2
		case 100:
			this.state2[KEY_LEFT] = value;
			break; // Num-4
		case 102:
			this.state2[KEY_RIGHT] = value;
			break; // Num-6
		default:
			return true;
		}
		return false; // preventDefault
	}

	public void keyDown(KeyEvent evt) {
		if (!this.setKey(evt.keyCode, (byte) 0x41) && evt.preventDefault) {
			evt.preventDefault();
		}
	}

	public void keyUp(KeyEvent evt) {
		if (!this.setKey(evt.keyCode, (byte) 0x40) && evt.preventDefault) {
			evt.preventDefault();
		}
	}

	public void keyPress(KeyEvent evt) {
		evt.preventDefault();
	}
}
